package com.woniu.woniu_bx.controller;

import com.woniu.woniu_bx.groups.UpdateGroup;
import com.woniu.woniu_bx.pojo.Manager;
import com.woniu.woniu_bx.pojo.ResponseEntity;
import com.woniu.woniu_bx.service.ManagerService;
import com.woniu.woniu_bx.vo.ManagerVo;
import org.apache.commons.math3.ml.neuralnet.UpdateAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.print.attribute.standard.MediaName;
import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;


/**
 * @ClassName ManagerController
 * @Description TODO
 * @Author 魏才林
 * @Date 2022/8/29 15:00
 * @Version 1.0
 **/
@RestController
@RequestMapping("/admin/manager")
@Validated
public class ManagerController {

    @Autowired
    private ManagerService managerService;

    /**
     * @return com.woniu.woniu_bx.pojo.ResponseEntity
     * @Description 获取分页的管理员列表
     * @Date 15:01 2022/8/29
     * @Author 魏才林
     * @Param [pageNum] 页码
     **/
    @GetMapping("/list")
    @PreAuthorize("hasAnyAuthority('admin:list')")
    public ResponseEntity list(@Min(1) @NotNull Integer pageNum) {
        //分页数量
        Integer pageSize = 10;
        return ResponseEntity.SUCCESS("ok", managerService.getAllManagers(pageNum, pageSize));
    }


    /**
     * @return
     * @Description 更新管理员
     * @Date 13:54 2022/8/31
     * @Author 魏才林
     * @Param
     **/
    @PostMapping("/update")
    @PreAuthorize("hasAnyAuthority('admin:update')")
    public ResponseEntity update(@RequestBody @Validated(UpdateGroup.class) Manager manager) {
        managerService.updateById(manager);
        return ResponseEntity.SUCCESS("ok", null);
    }

    /**
     * @return com.woniu.woniu_bx.pojo.ResponseEntity
     * @Description 新增管理员
     * @Date 15:10 2022/8/29
     * @Author 魏才林
     * @Param [id] 管理员id
     **/
    @PostMapping("/add")
    @PreAuthorize("hasAnyAuthority('admin:add')")
    public ResponseEntity lock(@RequestBody @Valid Manager manager) {
        managerService.addManager(manager);
        return ResponseEntity.SUCCESS("ok", null);
    }


    /**
     * @return com.woniu.woniu_bx.pojo.ResponseEntity
     * @Description 修改管理员状态
     * @Date 15:15 2022/8/29
     * @Author 魏才林
     * @Param [managerVo] 管理员的valueObject类，存储多参数
     **/
    @PostMapping("/updateStatus")
    @PreAuthorize("hasAnyAuthority('admin:update')")
    public ResponseEntity updateStatus(@RequestBody @Valid ManagerVo managerVo) {
        Manager manager = new Manager();
        manager.setId(managerVo.getId());

        //校验下status的值，是否跟业务匹配
        String status = managerVo.getStatus();
        if (!status.equalsIgnoreCase("y") && !status.equalsIgnoreCase("n")) {
            return ResponseEntity.FAIL("fail", null);
        }
        manager.setStatus(status);

        managerService.updateById(manager);
        return ResponseEntity.SUCCESS("ok", null);
    }
}
